package voice

import (
	"fmt"
	"syscall"
	"unsafe"
)

var (
	SND_SYNC        = 0x0000
	SND_ASYNC       = 0x0001
	SND_NODEFAULT   = 0x0002
	SND_MEMORY      = 0x0004
	SND_LOOP        = 0x0008
	SND_NOSTOP      = 0x0010
	SND_NOWAIT      = 0x00002000
	SND_ALIAS       = 0x00010000
	SND_ALIAS_ID    = 0x00110000
	SND_FILENAME    = 0x00020000
	SND_RESOURCE    = 0x00040004
	SND_PURGE       = 0x0040
	SND_APPLICATION = 0x0080
	SND_ALIAS_START = 0
)

func openPlayFile(fileName string, asyn bool) {
	funInDllFile, err := syscall.LoadLibrary("winmm.dll") // 调用的dll文件
	if err != nil {
		print("cant not call : syscall.LoadLibrary , errorInfo :" + err.Error())
	}
	defer syscall.FreeLibrary(funInDllFile)
	// 调用的dll里面的函数是：
	funName := "PlaySound"
	// 注册一长串调用代码，简化为 _win32Fun 变量.
	win32Fun, err := syscall.GetProcAddress(syscall.Handle(funInDllFile), funName)

	// 通过syscall.Syscall6()去调用win32的xxx函数，因为xxx函数有3个参数,故需取Syscall6才能放得下. 最后的3个参数,设置为0即可
	a3 := SND_FILENAME | SND_NODEFAULT
	if asyn {
		a3 = a3 | SND_ASYNC
	} else {
		a3 = a3 | SND_SYNC
	}
	//f := []rune(fileName)
	a1, _ := syscall.UTF16PtrFromString(fileName)
	_, _, err = syscall.Syscall(
		uintptr(win32Fun), // 调用的函数名
		3,                 // 指明该函数的参数数量
		//		uintptr(unsafe.Pointer(a1)), // 该函数的参数1. 可通过msdn查找函数名 查参数含义
		uintptr(unsafe.Pointer(a1)),
		0, // 该函数的参数2.
		uintptr(a3),
	)
	fmt.Println(err)
}
